{% extends 'admin/base.html' %}
{% set active_page = 'scales' %}

{% block title %}量表管理{% endblock %}

{% block page_title %}量表管理{% endblock %}

{% block page_subtitle %}管理系统量表、会员定制量表和量表使用情况{% endblock %}

{% block content %}

<div class="content-scroll">
    <!-- 量表统计卡片 -->
    <div class="row mb-4">
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-primary">
                        <i class="bi bi-clipboard-data" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="totalScales">-</h3>
                        <small class="text-muted">总量表数</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-primary me-2" id="scaleGrowth">+0%</span>
                    <small class="text-muted">较上月增长</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-success">
                        <i class="bi bi-check-circle" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="publishedScales">-</h3>
                        <small class="text-muted">已发布</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-success me-2" id="publishedRate">0%</span>
                    <small class="text-muted">发布率</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-info">
                        <i class="bi bi-graph-up" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="totalAssessments">-</h3>
                        <small class="text-muted">总测评次数</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-info me-2" id="dailyAssessments">日均 0</span>
                    <small class="text-muted">测评次数</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-warning">
                        <i class="bi bi-people" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="memberScales">-</h3>
                        <small class="text-muted">会员定制</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-warning me-2" id="memberRate">0%</span>
                    <small class="text-muted">定制率</small>
                </div>
            </div>
        </div>
    </div>

    <!-- 功能按钮栏 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="mb-0">量表列表管理</h4>
        <div class="btn-group">
            <button class="btn btn-success me-2" onclick="createScale()">
                <i class="bi bi-plus-circle me-1"></i>创建量表
            </button>
            <button class="btn btn-outline-primary" onclick="refreshStatistics()">
                <i class="bi bi-arrow-clockwise me-1"></i>刷新统计
            </button>
        </div>
    </div>

    <!-- 搜索筛选栏 -->
    <div class="search-filter-bar">
        <div class="row align-items-center">
            <div class="col-md-4">
                <div class="input-group">
                    <span class="input-group-text">
                        <i class="bi bi-search"></i>
                    </span>
                    <input type="text" class="form-control" id="searchInput" placeholder="搜索量表标题、描述...">
                </div>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="statusFilter">
                    <option value="">全部状态</option>
                    <option value="PUBLISHED">已发布</option>
                    <option value="DRAFT">草稿</option>
                    <option value="ARCHIVED">已归档</option>
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="publisherTypeFilter">
                    <option value="">全部类型</option>
                    <option value="system">系统内置</option>
                    <option value="member">会员定制</option>
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="memberLevelFilter">
                    <option value="">全部级别</option>
                    <option value="basic">基础会员</option>
                    <option value="professional">专业会员</option>
                    <option value="institution">机构会员</option>
                    <option value="research">科研会员</option>
                </select>
            </div>
            <div class="col-md-2">
                <button class="btn btn-outline-secondary w-100" onclick="searchScales()">
                    <i class="bi bi-funnel me-1"></i>筛选
                </button>
            </div>
        </div>
    </div>

    <!-- 量表列表 -->
    <div class="content-card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">量表列表</h5>
            <div class="d-flex align-items-center">
                <span class="text-muted me-3" id="scaleCount">共 0 个量表</span>
                <div class="btn-group btn-group-sm">
                    <button class="btn btn-outline-secondary active" title="列表视图">
                        <i class="bi bi-list"></i>
                    </button>
                    <button class="btn btn-outline-secondary" title="卡片视图">
                        <i class="bi bi-grid"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>
                                <input type="checkbox" class="form-check-input" id="selectAll">
                            </th>
                            <th>量表信息</th>
                            <th>发布者</th>
                            <th>状态</th>
                            <th>会员级别</th>
                            <th>使用次数</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="scalesTableBody">
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <div class="spinner-border text-primary" role="status">
                                    <span class="visually-hidden">加载中...</span>
                                </div>
                                <p class="mt-2 text-muted">正在加载量表数据...</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- 分页 -->
        <div class="card-footer">
            <nav aria-label="量表列表分页">
                <ul class="pagination justify-content-center mb-0" id="pagination">
                    <!-- 分页将通过JavaScript动态生成 -->
                </ul>
            </nav>
        </div>
    </div>
</div>

<!-- 设置会员级别模态框 -->
<div class="modal fade" id="memberLevelModal" tabindex="-1" aria-labelledby="memberLevelModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="memberLevelModalLabel">
                    <i class="bi bi-star me-2"></i>设置会员级别要求
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="memberLevelForm">
                    <input type="hidden" id="scaleIdForLevel">
                    <div class="mb-3">
                        <label for="scaleTitleDisplay" class="form-label">量表名称</label>
                        <input type="text" class="form-control" id="scaleTitleDisplay" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="minMemberLevel" class="form-label">最低会员级别要求</label>
                        <select class="form-select" id="minMemberLevel" required>
                            <option value="basic">基础会员</option>
                            <option value="professional">专业会员</option>
                            <option value="institution">机构会员</option>
                            <option value="research">科研会员</option>
                        </select>
                        <div class="form-text">选择使用此量表所需的最低会员级别</div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x-circle me-1"></i>取消
                </button>
                <button type="button" class="btn btn-primary" onclick="updateMemberLevel()">
                    <i class="bi bi-check-circle me-1"></i>保存设置
                </button>
            </div>
        </div>
    </div>
</div>

<!-- 更新状态模态框 -->
<div class="modal fade" id="statusModal" tabindex="-1" aria-labelledby="statusModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-warning text-white">
                <h5 class="modal-title" id="statusModalLabel">
                    <i class="bi bi-gear me-2"></i>更新量表状态
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="statusForm">
                    <input type="hidden" id="scaleIdForStatus">
                    <div class="mb-3">
                        <label for="scaleTitleForStatus" class="form-label">量表名称</label>
                        <input type="text" class="form-control" id="scaleTitleForStatus" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="newStatus" class="form-label">新状态</label>
                        <select class="form-select" id="newStatus" required>
                            <option value="PUBLISHED">已发布</option>
                            <option value="DRAFT">草稿</option>
                            <option value="ARCHIVED">已归档</option>
                        </select>
                        <div class="form-text">选择量表的新状态</div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x-circle me-1"></i>取消
                </button>
                <button type="button" class="btn btn-warning" onclick="updateStatus()">
                    <i class="bi bi-check-circle me-1"></i>更新状态
                </button>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/admin-scales.js') }}"></script>
{% endblock %} 